import os
import numpy as np


def read_points(file_path, dim=4, datatype=np.float32):
    suffix = os.path.splitext(file_path)[1]
    assert suffix in ['.bin', '.ply']
    if suffix == '.bin':
        points = np.fromfile(file_path, dtype=datatype)
        if len(points)%dim!=0:
            raise ValueError('Points dim is not divisible by dimension')
        return points.reshape(-1, dim)
    else:
        raise NotImplementedError


def write_points(points, file_path, datatype=np.float32):
    points32 = points.astype(datatype)
    suffix = os.path.splitext(file_path)[1]
    assert suffix in ['.bin', '.ply']
    if suffix == '.bin':
        with open(file_path, 'wb') as f:
            points32.tofile(f)
    else:
        raise NotImplementedError
